﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

using iTextSharp.text;
using iTextSharp.text.pdf;

namespace iPdf
{
    class SimpleActor<T> : Actor<ActionItem, T>
    {
        protected PdfReader reader;

        protected virtual void NullfyObjects()
        {
            reader = null;
        }

        protected virtual void ReadPdf(string fileName)
        {
            reader = new PdfReader(fileName);
        }

        protected void ReadPdf()
        {
            ReadPdf(workItem.Source);
        }

        protected virtual void PreProcess() { }

        protected virtual void Manipulate() { }

        protected virtual void PostProcess() { }

        protected virtual void ClosePdf()
        {
            SharpHelper.ClosePdf(reader, null);
        }

        protected override void Work()
        {
            base.Work();
            NullfyObjects();

            try
            {
                ReadPdf();
                PreProcess();
                Manipulate();
                ClosePdf();
                NullfyObjects();
                PostProcess();
            }
            finally
            {
                ClosePdf();
            }
        }

    }
}
